package com.sb.model;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

public class Role implements GrantedAuthority {

	private static final long serialVersionUID = 1L;

	private Integer id;

	private String authority;

	private User user;


	public Role(Integer id, String authority, User user) {
		super();
		this.id = id;
		this.authority = authority;
		this.user = user;
	}



	public Role(String authority) {
		Assert.hasText(authority,
				"A granted authority textual representation is required");
		this.authority = authority;
	}

	public Integer getId() {
		return id;
	}

	public String getAuthority() {
		return this.authority;
	}

	public User getUser() {
		return user;
	}

	public int compareTo(Object o) {
		if (o != null && o instanceof GrantedAuthority) {
			String rhsRole = ((GrantedAuthority) o).getAuthority();

			if (rhsRole == null) {
				return -1;
			}

			return authority.compareTo(rhsRole);
		}
		return -1;
	}

	public boolean equals(Object obj) {
		if (obj instanceof String) {
			return obj.equals(this.authority);
		}

		if (obj instanceof GrantedAuthority) {
			GrantedAuthority attr = (GrantedAuthority) obj;

			return this.authority.equals(attr.getAuthority());
		}

		return false;
	}

	public int hashCode() {
		return this.authority.hashCode();
	}

	public String toString() {
		return this.authority;
	}

}
